﻿using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ArduinoSketch.Core
{
    public partial class SketchProperties : Form
    {
        public SketchProperties()
        {
            InitializeComponent();

            Hardwares _hardware = Utils.LoadHardwardBoard();
            foreach (var profile in _hardware.Profile)
            {
                this.cmdProfile.Items.Add(profile);
            }
        }

        private void LoadFileProperties(List<ProjectFileEntity> files)
        {
            int _idx = 1;
            foreach (var _file in files)
            {
                string file_name = string.Format(@"{0}\{1}", _file.FilePath, _file.FileName);
                FileInfo _info = new FileInfo(file_name);
                ListViewItem _item = new ListViewItem(_idx.ToString());
                _item.SubItems.Add(_file.FileName);
                _item.SubItems.Add(_info.CreationTime.ToString());
                _item.SubItems.Add(_info.LastWriteTime.ToString());

                this.listFiles.Items.Add(_item);
            }
        }

        public DialogResult ShowDialog(ProjectEntity project)
        {
            this.lbName.Text = project.ProjectName;
            this.lbLocation.Text = project.ProjectPath;

            int _idx = 0;
            if (!string.IsNullOrEmpty(project.HardwareProfile))
            {
                foreach (var item in cmdProfile.Items)
                {
                    if (item.ToString().Equals(project.HardwareProfile))
                        break;
                    _idx++;
                }
                this.cmdProfile.SelectedIndex = this.cmdProfile.Items.Count == _idx ? -1 : _idx;
            }

            this.LoadFileProperties(project.ProjectFiles);
            

            DialogResult _result = this.ShowDialog();
            if (_result == DialogResult.OK)
            {
                project.HardwareProfile = this.cmdProfile.Text;
            }
            return _result;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {

        }
    }
}
